var utils = require("../utils.js");

var watch_video = {
  seconds: 0.5 * 60,
  init() {
    sleep(5 * 1000);
    if (utils.exists_text("视频")) {
      utils.click_text("视频");
      sleep(2 * 1000);
      this.execute(this.seconds);
    } else {
      console.log("watch_video init", "没找到【视频】");
      this.end();
    }
  },
  execute(seconds) {
    var start_time = Date.now();
    while (true) {
      swipe(
        device.width / 2,
        device.height / 1.2,
        device.width / 2,
        device.height / 1.2 - 1000,
        1000
      );
      var interval = random(5, 15);
      sleep(interval * 1000);
      var end_time = Date.now();
      console.log(
        "watch_video execute:",
        "视频已看",
        ((end_time - start_time) / 1000).toFixed(2),
        "s",
        ((end_time - start_time) / 1000 / 60).toFixed(2),
        "m"
      );
      if (end_time - start_time > seconds * 1000) {
        break;
      }
    }
    this.after();
  },
  after() {
    this.end();
  },
  end() {
    sleep(2 * 1000);
    if (!utils.click_text("百度")) {
      console.log("watch_video end:", "没找到【百度】");
    }
    console.log("watch_video end:");
    this.next();
  },
  next() {
    console.log("watch_video next:");
  },
};

module.exports = watch_video;
